/*
 * This is a Presentation Control Bean for the browse and select use case.  It
 * is intended to hold JSF action methods for the Browse and
 * Select use case.
 *
 * It will also hold fields accessed by the JSF pages.
 *
 * @since 8/4/08 -- had to add the imports
 * @author levi
 */
package application.jsf;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;

import application.BrowseSelectController;
import business.productSubsystem.MPTestProductSubsystemFacade;
import business.subsystemExternalInterfaces.Catalog;
import business.subsystemExternalInterfaces.ProductSubsystem;
import business.subsystemExternalInterfaces.Product;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Named("bsControllerPCB")
@SessionScoped
public class BrowseSelectPCB implements Serializable {

    private ProductSubsystem stubProdSS = new MPTestProductSubsystemFacade();
    private BrowseSelectController bsController = new BrowseSelectController(stubProdSS);
    private String selectedCatalogName;
    private String selectedProductName;
    private int purchaseQuantity;

    public List<Catalog> getCatalogs() {
        List<String> catalogNames = new ArrayList<String>();
        catalogNames = bsController.getCatalogNames();

        List<Catalog> catalogs = new ArrayList<Catalog>();
        Catalog catalog = null;
        for (String catalogName : catalogNames) {
            catalog = stubProdSS.retrieveCatalog(catalogName);
            catalogs.add(catalog);
        }

        return catalogs;
    }

    public String getSelectedCatalogName() {
        return selectedCatalogName;
    }

    public void setSelectedCatalogName(String selectedCatalogName) {
        this.selectedCatalogName = selectedCatalogName;
        Catalog catalog = this.stubProdSS.retrieveCatalog(selectedCatalogName);
        this.bsController.setCat2Browse(catalog);
    }

    public List<Product> getProducts() {
        return bsController.getProducts(selectedCatalogName);
    }

    public String getSelectedProductName() {
        return selectedProductName;
    }

    public void setSelectedProductName(String selectedProductName) {
        this.selectedProductName = selectedProductName;
    }

    public Product getSelectedProduct() {
        return bsController.getProduct(selectedProductName);
    }

    public int getPurchaseQuantity() {
        return purchaseQuantity;
    }

    public void setPurchaseQuantity(int purchaseQuantity) {
        this.purchaseQuantity = purchaseQuantity;
    }

    public String addToCart() {
        this.bsController.add2Cart(selectedProductName, purchaseQuantity);
        return "cartItems";
    }

    public List<String[]> getCartDisplayItems() {
        return bsController.getCartDisplayList();
    }

    public String startOver() {
        this.selectedCatalogName = null;
        this.selectedProductName = null;
        this.purchaseQuantity = 0;
        this.bsController.setCat2Browse(null);
        this.bsController.setShoppingCart(null);

        return "catalogSelection";
    }
}
